# import re
#
# from django.conf import settings
# from rest_framework.exceptions import ValidationError
# from rest_framework.response import Response
# from rest_framework_jwt.views import JSONWebTokenAPIView
# from rest_framework_jwt.serializers import JSONWebTokenSerializer
# from django.contrib.auth import authenticate
# from rest_framework import serializers
# from django.utils.translation import ugettext as _
# from rest_framework_jwt.settings import api_settings
#
# from apps.users.models import User
#
# jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
# jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER
#
#
# class LoginSerializer(serializers.ModelSerializer):
#     username = serializers.CharField()
#
#     class Meta:
#         model = User
#         fields = ('username', 'password')
#
#     def validate(self, attrs):
#         # username可能是邮箱，手机号，用户名
#         username = attrs.get('username')
#         password = attrs.get('password')
#         # 如果是手机号
#         if re.match('^1[3-9]\d{9}$', username):
#             # 以手机号登录
#             user = User.objects.filter(mobile=username).first()
#         elif re.match('^.+@.+$', username):
#             # 以邮箱登录
#             user = User.objects.filter(email=username).first()
#         else:
#             # 以用户名登录
#             user = User.objects.filter(username=username).first()
#         # 如果user有值并且密码正确
#         if user:
#             if not user.is_active:
#                 msg = _('User account is disabled.')
#                 raise serializers.ValidationError(msg)
#
#             payload = jwt_payload_handler(user)
#
#             return {
#                 'user':user
#             }
#
#         else:
#             raise ValidationError('用户名或密码错误')

    # def validate(self, attrs):
    #     credentials = {
    #         self.username_field: attrs.get(self.username_field),
    #         'password': attrs.get('password')
    #     }
    #     if all(credentials.values()):
    #         user = authenticate(**credentials)
    #
    #         if user:
    #             if not user.is_active:
    #                 msg = _('User account is disabled.')
    #                 raise serializers.ValidationError(msg)
    #
    #             payload = jwt_payload_handler(user)
    #
    #             return {
    #                 'token': jwt_encode_handler(payload),
    #                 'user':user
    #             }
    #         else:
    #             msg = _('Unable to log in with provided credentials.')
    #             raise serializers.ValidationError(msg)
    #     else:
    #         msg = _('Must include "{username_field}" and "password".')
    #         msg = msg.format(username_field=self.username_field)
    #         raise serializers.ValidationError(msg)

# class UserJsonWebTokenAPIView(JSONWebTokenAPIView):
#     # 用户登录 POST /authorizations/
#     serializer_class = LoginSerializer
#     # serializer_class = JSONWebTokenSerializer
#     # 管理员登录POSTmeiduo_admin/authorizations/
#
#
# admin_jwt_token = UserJsonWebTokenAPIView.as_view()
